package top.chatgqt.SmartChargeStation.demo.websocket.spring.conf;

import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import top.chatgqt.SmartChargeStation.demo.websocket.spring.handler.ImoocHandler;
import top.chatgqt.SmartChargeStation.demo.websocket.spring.interceptor.ImoocInterceptor;

/**
 * 基于Spring注解的 WebSocket配置类
 */
@Configuration
public class WebSocketConf implements WebSocketConfigurer {

    @Resource
    private ImoocInterceptor interceptor;
    @Resource
    private ImoocHandler handler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        /*

        处理器：WebSocket握手之后触发
            主要用于业务处理

        拦截器：握手之后，握手之前都能触发
            主要用于身份验证
         */


        registry
                // 处理器配置
                .addHandler(handler,"/ws/server")
                // 拦截器配置
                .addInterceptors(interceptor)
                // 跨域 所有的地址都能连接（关闭跨域）
                .setAllowedOrigins("*");
    }
}
